package com.example.literaturesystem.controller.team;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.literaturesystem.domain.team.Team;
import com.example.literaturesystem.domain.user.User;
import com.example.literaturesystem.service.team.ITeamService;
import com.example.literaturesystem.service.user.IUserService;
import com.google.gson.Gson;

@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private ITeamService teamService;
    private IUserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String get() {
        Team team = new Team();
        team.setIfuse("y");
        team.setUserRole("c");
        List<Team> teamList = this.teamService.query(team);
        Gson gson = new Gson();
        return gson.toJson(teamList);
    }

    @RequestMapping(value = "/alone", method = RequestMethod.GET)
    public String getAlone(){
    	 Team team = new Team();
    	 team.setUserRole("b");
         team.setIfuse("y");
         List<Team> teamList = this.teamService.query(team);
         Gson gson = new Gson();
         return gson.toJson(teamList);
    }
    @RequestMapping(value = "/query/{param}/{value}", method = RequestMethod.GET)
    public String query(@PathVariable("param") String param, @PathVariable("value") String value) {
        Team team = new Team();
        System.out.println(param);
        switch (param) {
            case "teamNo": {
                team.setTeamNo(value);
                break;
            }
            case "ifuse" :{
            	team.setIfuse(value);
            	break;
            }

        }
        List<Team> teamList = this.teamService.query(team);
        for(int i =0;i<teamList.size();i++)
        {
        	if(teamList.get(i).getUserRole().equals("b"))
        		teamList.get(i).setUserRole("独立团队");
        	else
        		teamList.get(i).setUserRole("团体团队");
        }
       /* int flag = teamList.size();

        Map<String, Object> resultMap = new HashMap();
        if (flag > 0) {
            resultMap.put("code", "0000");
            resultMap.put("data", teamList.get(0));
        } else {
            resultMap.put("code", "9999");
            resultMap.put("data", "error");
        }*/
        Gson gson = new Gson();
        return gson.toJson(teamList);
    }

    @RequestMapping(method = RequestMethod.PUT)
    public String update(@RequestBody Team team) {
        System.out.println(team.getTeamNo()+"---"+team.getUserEmail());
        Map<String, String> resultMap = new HashMap();
        if (!StringUtils.isEmpty(team.getTeamNo())) {
            int flag = this.teamService.update(team);
            if (flag == 1) {
                resultMap.put("code", "0000");
                resultMap.put("data", "success");
            }
        } else {
            resultMap.put("code", "9999");
            resultMap.put("data", "error");
        }
        Gson gson = new Gson();
        return gson.toJson(resultMap);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String add(@RequestBody Team team) {
    	/*Team test = new Team();
    	test.setTeamNo(team.getTeamNo());
    	List<Team> result = this.teamService.query(test);
    	if(result.size()==0)
    	{
    		User user = new User();
    		user.setUserNo(team.getTeamNo());
    		List<User> userlist = this.userService.query(user);
    		if(userlist.size()==0)
    		{
    			
    		}
    	}*/
        team.setPassword("111111");
        System.out.println("asdsa");
        team.setIfuse("y");
        //通知信息添加至数据库
        int flag = this.teamService.insert(team);

        Map<String, String> resultMap = new HashMap();
        if (flag == 1) {
            resultMap.put("code", "0000");
            resultMap.put("data", "success");
        } else {
            resultMap.put("data", "error");
            resultMap.put("code", "9999");
        }
        Gson gson = new Gson();
        return gson.toJson(resultMap);
    }





}
